{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "151572c1",
   "metadata": {},
   "source": [
    "#   [Py4Hw User Guide](../UserGuide.ipynb)> 1.5 Ports\n",
    "###  [Next: 1.6 Wires](wires.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e98f1cc3",
   "metadata": {},
   "source": [
    "All circuits have ports, either input or output.\n",
    "\n",
    "Bidirectional ports are supported, but not recommended, in py4hw.\n",
    "\n",
    "The InPort and OutPort classes have similar fields and methods.\n",
    "\n",
    "| Field | Description |\n",
    "| :-- | :-- |\n",
    "| name | The name of the port |\n",
    "| parent | The parent circuit (Logic derived object) containing the port |\n",
    "| wire | The wire currently connected to the port |\n",
    "\n",
    "| Field | Description |\n",
    "| :-- | :-- |\n",
    "| getFullPath | Returns the name (with the full path) of the port |\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c49aa137",
   "metadata": {},
   "outputs": [],
   "source": [
    "import py4hw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0de31be4",
   "metadata": {},
   "outputs": [],
   "source": [
    "sys = py4hw.HWSystem()\n",
    "\n",
    "a = sys.wire('a', 32)\n",
    "reset = sys.wire('reset')\n",
    "inc = sys.wire('inc', 1)\n",
    "\n",
    "cnt = py4hw.Counter(sys, 'count', reset=reset, inc=inc, q=a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "81855066",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['reset', 'inc', 'q']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "py4hw.CircuitAnalysis.getAllPortNames(cnt)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f0a7b82",
   "metadata": {},
   "source": [
    "Input ports is just a list of InPort objects "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ff90adcd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<py4hw.base.InPort at 0x21029d13608>, <py4hw.base.InPort at 0x21029e7fd48>]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cnt.inPorts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8e5a4c57",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'reset',\n",
       " 'parent': <py4hw.logic.arithmetic.Counter at 0x21029f20b08>,\n",
       " 'wire': <py4hw.base.Wire at 0x21029e6e508>}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cnt.inPorts[0].__dict__"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09dc2b82",
   "metadata": {},
   "source": [
    "Similarly, output ports is another list of OutPort objects"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3c2d2104",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<py4hw.base.OutPort at 0x21029fcd108>]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cnt.outPorts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "dafd081f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'q',\n",
       " 'parent': <py4hw.logic.arithmetic.Counter at 0x21029f20b08>,\n",
       " 'wire': <py4hw.base.Wire at 0x21029f247c8>}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cnt.outPorts[0].__dict__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "031bcaaa",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
